package weibo.service;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import weibo.database.UserDB;
import weibo.model.UserMod;

/**
 * Servlet implementation class userupdate
 */
@WebServlet("/userupdate")
public class userupdate extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public userupdate() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		//登录控制
				HttpSession session=request.getSession();
				List<UserMod> user=(List<UserMod>) session.getAttribute("user");
				if (user==null){
					response.setCharacterEncoding("utf8");
					response.setContentType("text/html;");
					response.getWriter().append("未登录，请点击 	 <a href='./userlogin'>登录</a>");
				}else{
					doPost(request,response);
				}
	}

	/**
	 * 用户信息更新(图片路径更新)
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		UserDB db=new UserDB();
		String user_name=request.getParameter("user_name");
		String borndate=request.getParameter("borndate");
		String sex=request.getParameter("sex");
//		String user_head=request.getParameter("user_head");
		String simple=request.getParameter("simple");
		HttpSession session=request.getSession();
		List<UserMod> user=(List<UserMod>) session.getAttribute("user");
		if (user!=null){
			if (user_name==null){
				//user.get(0).setUser_name(user_name);
			}else if (user_name.length()>0){
				user.get(0).setUser_name(user_name);
			}
			
			if (borndate==null){
				user.get(0).setBorndate(null);
				//System.out.println("borndate:"+borndate);
			}else if (borndate.length()>0){
				user.get(0).setBorndate(borndate);
				//System.out.println("borndate:"+borndate);
			}
			if (sex==null){
				user.get(0).setSex(null);
			}else if (sex.length()>0){
				user.get(0).setSex(sex);
			}
//			System.out.println("simple:"+simple);
//			if (user_head==null){
//				user.get(0).setUser_head(null);
//			}else if (user_head.length()>0){
//				user.get(0).setUser_head(user_head);
//			}
			if (simple==null){
				user.get(0).setSimple(null);
				//simple=user.get(0).getSimple();
			}else if (simple.length()>0){
				user.get(0).setSimple(simple);
			}
			
			boolean res=db.UserUpdate(user);
			if (res){
				response.getWriter().append("success");
			}else{
				response.getWriter().append("error");
				System.out.println(user.get(0).getLogin_name()+" 用户修改信息失败");
			}
		}else{
			response.getWriter().append("error");
			System.out.println("未登录用户修改信息");
		}
		
	}

}
